package server

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

type Response struct {
	Success bool        `json:"success"`
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func NewResponse(c *gin.Context, success bool, code int, data interface{}, msg string) {
	c.JSON(code, Response{
		Success: success,
		Code:    code,
		Message: msg,
		Data:    data,
	})
}

func ResponseSuccess(c *gin.Context, data interface{}) {
	NewResponse(c, true, http.StatusOK, data, "success")
}

func ResponseFailed(c *gin.Context, code int, err error) {
	if code == 0 {
		code = http.StatusInternalServerError
	}

	var msg string

	if err != nil {
		msg = err.Error()
		var url string
		if c.Request != nil {
			url = c.Request.URL.String()
		}
		logrus.Warnf("url: %s, error: %v", url, msg)
	}

	NewResponse(c, false, code, nil, msg)
}
